package com.originskyseed.platform.user.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.user.mapper.PlayerRelateRechargeMapper;
import com.originskyseed.platform.user.mapper.PlayerRelateViewLiveMapper;
import com.originskyseed.platform.user.model.PlayerRelateRecharge;
import com.originskyseed.platform.user.model.PlayerRelateViewLive;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.HighQualityUserMapper;
import com.originskyseed.platform.user.mapper.PlayerMapper;
import com.originskyseed.platform.user.model.HighQualityUser;
import com.originskyseed.platform.user.model.Player;
import com.originskyseed.platform.user.service.IHighQualityUserService;

/**
 * 优质用户表Service接口实现类（OSI_HIGH_QUALITY_USER）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("highQualityUserService")
public class HighQualityUserServiceImpl implements IHighQualityUserService {

	@Resource
	private HighQualityUserMapper highQualityUserMapper;

	@Resource
	private PlayerMapper playerMapper;

	@Resource
	private PlayerRelateRechargeMapper playerRelateRechargeMapper;

	@Resource
	private PlayerRelateViewLiveMapper playerRelateViewLiveMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.highQualityUserMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.highQualityUserMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(HighQualityUser record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.highQualityUserMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(HighQualityUser record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.highQualityUserMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(HighQualityUser record) throws Exception {
		record.setUpdateTime(new Date());
		return this.highQualityUserMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(HighQualityUser record) throws Exception {
		record.setUpdateTime(new Date());
		return this.highQualityUserMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public HighQualityUser selectByPrimaryKey(String id) throws Exception {
		return this.highQualityUserMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<HighQualityUser> selectList(HighQualityUser record) throws Exception {
		return this.highQualityUserMapper.selectList(record);
	}

	/**
	 * 统计优质用户
	 *
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	public int insertHighQualityUser(String tenantCode) throws Exception{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		// 要新增的集合
		List<HighQualityUser> highQualityUserList = new ArrayList<>();
		List<Player> playerList = this.playerMapper.statisticsHighQualityUser(500);
		for (Player player:playerList) {
			HighQualityUser highQualityUser = new HighQualityUser();
			highQualityUser.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			highQualityUser.setName(player.getBelongBusinessName());
			highQualityUser.setAmount(player.getAmount());
			highQualityUser.setViewTimes(player.getLiveTime());
			highQualityUser.setNickName(player.getNickName());
			highQualityUser.setPlayerId(player.getPlayerId());
			highQualityUser.setUserId(player.getUserId());
			highQualityUser.setEquipmentCode(player.getEquipmentCode());
			highQualityUser.setChannelSubId(player.getChannelSubId());
			highQualityUser.setCreateTime(new Date());
			highQualityUserList.add(highQualityUser);

			// 查询充值达到10元，达成时间
			if(player.getAmount()!=null && player.getAmount().compareTo(new BigDecimal(10)) >=0 ){
				highQualityUser.setAddTime(new Date());
				PlayerRelateRecharge playerRelateRecharge =	 new PlayerRelateRecharge();
				playerRelateRecharge.setPlayerId(player.getPlayerId());
				List<PlayerRelateRecharge> playerRelateRechargeList = this.playerRelateRechargeMapper.selectList(playerRelateRecharge);
				BigDecimal amount = BigDecimal.ZERO;
				for (PlayerRelateRecharge playerRelateRecharge1:playerRelateRechargeList) {
					amount = amount.add(playerRelateRecharge1.getAmount());
					if (amount.compareTo(new BigDecimal(10)) >= 0) {
						highQualityUser.setCompletionTime(playerRelateRecharge1.getRechargeTime());
						break;
					}
				}
			}

			// 查询时间达到3分钟，达成时间
			if(player.getLiveTime()!=null && player.getLiveTime().compareTo(new BigDecimal(180)) >=0 ){
				highQualityUser.setViewAddTime(new Date());
				PlayerRelateViewLive playerRelateViewLive = new PlayerRelateViewLive();
				playerRelateViewLive.setPlayerId(player.getPlayerId());
				List<PlayerRelateViewLive> playerRelateViewLiveList = this.playerRelateViewLiveMapper.selectList(playerRelateViewLive);
				BigDecimal liveTime = BigDecimal.ZERO;
				for (PlayerRelateViewLive playerRelateViewLive1:playerRelateViewLiveList) {
					if(playerRelateViewLive1.getLiveTime()!=null) {
						liveTime = liveTime.add(playerRelateViewLive1.getLiveTime());
						if (liveTime.compareTo(new BigDecimal(180)) >= 0) {
							highQualityUser.setViewCompletionTime(playerRelateViewLive1.getStartTime());
							break;
						}
					}
				}
			}
			// 修改玩家为优质用户
			Player updatePlayer = new Player();
			updatePlayer.setId(player.getId());
			updatePlayer.setIsHighQuality(1);
			this.playerMapper.updateByPrimaryKeySelective(updatePlayer);
		}
		if(highQualityUserList.size()>0){
			highQualityUserMapper.insertBatch(highQualityUserList);
		}
		return 1;
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<HighQualityUser> selectListByPage(Map<String, Object> params, BasePage<HighQualityUser> page) throws Exception {
		params.put("page", page);
		List<HighQualityUser> list = this.highQualityUserMapper.selectListByPage(params);
		if(list!=null&&list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				HighQualityUser highQualityUser=list.get(i);
				if(highQualityUser.getViewTimes()!=null) {
					BigDecimal viewTimes = highQualityUser.getViewTimes().divide(new BigDecimal(60),2, BigDecimal.ROUND_HALF_UP);
					highQualityUser.setViewTimes(viewTimes);
				}
			}
		}
		page.setResults(list);
		return page;
	}

}